{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "14dafb90-f1ee-4f4e-a853-dec409b70419",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *  # 导入sympy 包中所有的函数\n",
    "from sympy.abc import x,y # 引入默认的符号变量\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a30321c-ee43-40d3-aa03-6a71b22659b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = symbols('c ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0dac18b5-3fa1-4fb9-a97b-d478ced52208",
   "metadata": {},
   "outputs": [],
   "source": [
    "Vz = x**2 + c*x**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3ccfd3c1-c80c-4a93-8c1c-bf0ee889b6cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 3 c x^{2} + 2 x$"
      ],
      "text/plain": [
       "3*c*x**2 + 2*x"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Vzdx = diff(Vz,x)\n",
    "Vzdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "45d0d166-36be-440e-9fd5-6fc03e96bae6",
   "metadata": {},
   "outputs": [],
   "source": [
    "f_func = lambdify(x, Vz.subs(c,1), modules=['numpy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2614393b-c2bf-4a12-9318-734c3d2937b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "x= np.linspace(-10,10,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8cd8516f-2dde-4967-85be-1726e30b29c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "F_vals = f_func(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ea34e1d1-e07e-4c6f-96e3-c31a0c1e8cee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-9.00000000e+02, -8.44609651e+02, -7.91536939e+02, -7.40732393e+02,\n",
       "       -6.92146545e+02, -6.45729924e+02, -6.01433063e+02, -5.59206492e+02,\n",
       "       -5.19000741e+02, -4.80766341e+02, -4.44453823e+02, -4.10013717e+02,\n",
       "       -3.77396555e+02, -3.46552867e+02, -3.17433183e+02, -2.89988035e+02,\n",
       "       -2.64167952e+02, -2.39923467e+02, -2.17205109e+02, -1.95963409e+02,\n",
       "       -1.76148898e+02, -1.57712107e+02, -1.40603567e+02, -1.24773807e+02,\n",
       "       -1.10173359e+02, -9.67527535e+01, -8.44625213e+01, -7.32531931e+01,\n",
       "       -6.30752995e+01, -5.38793712e+01, -4.56159390e+01, -3.82355336e+01,\n",
       "       -3.16886857e+01, -2.59259259e+01, -2.08977851e+01, -1.65547939e+01,\n",
       "       -1.28474831e+01, -9.72638331e+00, -7.14202529e+00, -5.04493976e+00,\n",
       "       -3.38565741e+00, -2.11470897e+00, -1.18262515e+00, -5.39936659e-01,\n",
       "       -1.37174211e-01,  7.51314801e-02,  1.46449703e-01,  1.26249744e-01,\n",
       "        6.40008904e-02,  9.17243035e-03,  1.12336507e-02,  1.19653839e-01,\n",
       "        3.83902282e-01,  8.53448267e-01,  1.57776108e+00,  2.60631001e+00,\n",
       "        3.98856435e+00,  5.77399338e+00,  8.01206638e+00,  1.07522527e+01,\n",
       "        1.40440215e+01,  1.79368421e+01,  2.24801839e+01,  2.77235162e+01,\n",
       "        3.37163081e+01,  4.05080290e+01,  4.81481481e+01,  5.66861349e+01,\n",
       "        6.61714585e+01,  7.66535882e+01,  8.81819934e+01,  1.00806143e+02,\n",
       "        1.14575507e+02,  1.29539554e+02,  1.45747754e+02,  1.63249576e+02,\n",
       "        1.82094488e+02,  2.02331962e+02,  2.24011465e+02,  2.47182466e+02,\n",
       "        2.71894437e+02,  2.98196844e+02,  3.26139159e+02,  3.55770850e+02,\n",
       "        3.87141386e+02,  4.20300237e+02,  4.55296872e+02,  4.92180761e+02,\n",
       "        5.31001372e+02,  5.71808175e+02,  6.14650639e+02,  6.59578233e+02,\n",
       "        7.06640427e+02,  7.55886691e+02,  8.07366492e+02,  8.61129301e+02,\n",
       "        9.17224587e+02,  9.75701820e+02,  1.03661047e+03,  1.10000000e+03])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F_vals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a3ba941b-0073-4087-95a7-d56633c49d10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle c x^{3} + x^{2}$"
      ],
      "text/plain": [
       "c*x**3 + x**2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Vz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af270969-7785-4b39-89e6-98ab59d3dfb3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
